package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.SeniorRuleValueEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 公海高级模式 - 自动退回规则和转移 日志数据获取
 *
 * @author : 杨磊
 * @date 创建时间：2022-05-31 下午4:24:22
 */
@Component
public class OperateSeniorBackRuleValue {

    /**
     * 获取不退回条件 jsonDta
     * @param jsonDataArr
     * @return
     */
    public JSONArray getJsonDataArr(JSONArray jsonDataArr) {
        if (jsonDataArr == null){
            return null;
        }
        return jsonDataArr;
    }
    /**
     * 获取规则名称
     * @param ruleValueArr
     * @return
     */
    public String getRuleNameStr(JSONArray ruleValueArr) {
        if (CollectionUtils.isEmpty(ruleValueArr)){
            return null;
        }
        List<String> ruleNameList = new ArrayList<>();
        for (Object o : ruleValueArr){
            JSONObject jsonObject = (JSONObject)o;
            ruleNameList.add(jsonObject.getString("name"));
        }
        return StringUtils.join(ruleNameList, "，");
    }

    /**
     * 获取适用范围
     * @param ruleValueArr
     * @return 数组 - [A组，B组]
     */
    public String getApplyScore(JSONArray ruleValueArr) {
        if (CollectionUtils.isEmpty(ruleValueArr)){
            return null;
        }
        List<String> applyScopeList = new ArrayList<>();
        for (Object o : ruleValueArr){
            JSONObject jsonObject = (JSONObject)o;
            JSONArray applyScopeArr = FastJsonHelper.getJsonArrayOrDefault(jsonObject, SeniorRuleValueEnum.APPLY_SCOPE.getAlias(), new JSONArray());
            for (Object applyObj : applyScopeArr){
                JSONObject applyJson = (JSONObject)applyObj;
                applyScopeList.add(applyJson.getString("label"));
            }
        }
        return StringUtils.join(applyScopeList, "，");
    }

    /**
     * 获取筛选条件类型
     * @param ruleValue
     * @return 1：所有客户统一设置，2：根据客户条件设置
     */
    public Integer getFilterType(JSONObject ruleValue) {
        if (ruleValue == null){
            return null;
        }
        JSONObject filterScreen = FastJsonHelper.getJsonObjectOrDefault(ruleValue, SeniorRuleValueEnum.FILTER_SCREEN.getAlias(), new JSONObject());
        return filterScreen.getInteger("filterType");
    }

    /**
     * 获取筛选条件
     * @param ruleValue
     * @return
     */
    public JSONArray getFilterRuleArr(JSONArray ruleValue) {
        JSONArray filterScreenList = new JSONArray();
        if (CollectionUtils.isNotEmpty(ruleValue)){
            for (Object o : ruleValue){
                JSONObject jsonObject = (JSONObject)o;
                JSONObject filterScreen = FastJsonHelper.getJsonObjectOrDefault(jsonObject, SeniorRuleValueEnum.FILTER_SCREEN.getAlias(), new JSONObject());
                JSONArray filterRule = new JSONArray();
                Integer filterType = filterScreen.getInteger("filterType");
                if (Objects.equals(filterType,BasicConstant.TWO)){
                    filterRule = FastJsonHelper.getJsonArrayOrDefault(filterScreen, "filterRule", new JSONArray());
                }else {
                    //代表所有线索客户统一设置
                    JSONObject temp = new JSONObject();
                    temp.put("filterType",1);
                    filterRule.add(temp);
                }
                filterScreenList.addAll(filterRule);
            }
        }
        return filterScreenList;
    }

    /**
     * 获取触发条件类型
     * @param ruleValue
     * @return 1.定时退回，2.当日退回
     */
    public Integer getTriggerScreenType(JSONObject ruleValue) {
        if (ruleValue == null){
            return null;
        }
        JSONObject triggerScreen = FastJsonHelper.getJsonObjectOrDefault(ruleValue, SeniorRuleValueEnum.TRIGGER_SCREEN.getAlias(), new JSONObject());
        return triggerScreen.getInteger("triggerType");
    }

    /**
     * 获取触发条件中退回类型
     * @param ruleValue
     * @return
     */
    public Integer getChooseType(JSONObject ruleValue) {
        if (ruleValue == null){
            return null;
        }
        JSONObject triggerScreen = FastJsonHelper.getJsonObjectOrDefault(ruleValue, SeniorRuleValueEnum.TRIGGER_SCREEN.getAlias(), new JSONObject());
        JSONArray triggerScreenRule = FastJsonHelper.getJsonArrayOrDefault(triggerScreen, "rule", new JSONArray());
        JSONObject jsonObject  = (JSONObject)triggerScreenRule.get(BasicConstant.ZERO);
        return jsonObject.getInteger("chooseType");
    }

    /**
     * 获取触发条件
     * @param ruleValue
     * @return
     */
    public JSONArray getTriggerScreen(JSONObject ruleValue) {
        if (ruleValue == null){
            return null;
        }
        JSONObject triggerScreen = FastJsonHelper.getJsonObjectOrDefault(ruleValue, SeniorRuleValueEnum.TRIGGER_SCREEN.getAlias(), new JSONObject());
        return FastJsonHelper.getJsonArrayOrDefault(triggerScreen, "rule", new JSONArray());
    }

    /**
     * 获取目标公海名称
     * @param ruleValueArr
     * @return
     */
    public String getObjectGroup(JSONArray ruleValueArr) {
        List<String> objectGroupList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(ruleValueArr)){
            for (Object o : ruleValueArr){
                JSONObject jsonObject = (JSONObject)o;
                JSONObject objectGroupJson = jsonObject.getJSONObject(SeniorRuleValueEnum.OBJECTIVE_GROUP.getAlias());
                objectGroupList.add(objectGroupJson.getString("label"));
            }
        }
        return StringUtils.join(objectGroupList, "，");
    }
}
